#include <math.h>
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;


class Figure{
public:
    virtual string getName() const = 0;
    virtual double getArea() const = 0;
};

void display(Figure & fig) {
    cout << fig.getName()
         << "的面积是:"
         << fig.getArea() << endl ;
}

class Rectangle
: public Figure
{
public:
    Rectangle(double len,double wid)
    : _length(len)
    , _width(wid)
    {}
    
    string getName() const override{
        return "矩形";
    }

    double getArea() const override{
        return _length * _width;
    }

private:
    double _length;
    double _width;
};


class Circle
: public Figure
{
public:
    Circle(double rad)
    : _radius(rad)
    {}

    string getName() const override{
        return "圆形";
    }

    double getArea() const override{
        return _radius * _radius * PI;
    }
    
private:
    static constexpr double PI = 3.14;
    double _radius;
};

class Triangle
: public Figure
{
public:
    Triangle(double a,double b,double c)
    : _a(a)
    , _b(b)
    , _c(c)
    {}

    string getName() const override{
        return "三角形";
    }

    double getArea() const override{
       //海伦公式
       double p = (_a + _b + _c)/2;
       return sqrt(p * (p - _a) * (p - _b) * (p - _c));
    }

private:
    double _a;
    double _b;
    double _c;
};

void test0(){
    Rectangle re(3,4);
    Circle ci(2);
    Triangle tr(3,4,5);
    /* Figure f; */

    Figure * fg = &tr;
    cout << fg->getName() << endl;
    cout << fg->getArea() << endl;

    Figure & ref = ci;
    cout << ref.getName() << endl;
    cout << ref.getArea() << endl;

    cout << endl;
    display(re);
    display(ci);
    display(tr);
    
}

int main(void){
    test0();
    return 0;
}
